package com.zhny.aiot.control.cabinet.plugin;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zhny.aiot.control.cabinet.plugin.model.AdjustableParam;
import com.zhny.aiot.control.cabinet.plugin.model.ControlCabinetStoreEntry;
import com.zhny.aiot.plugin.ICenterFacilities;
import com.zhny.aiot.plugin.IDevice;
import com.zhny.aiot.plugin.core.utils.ConfigUtils;
import com.zhny.aiot.plugin.modbus.ModemServer;
import com.zhny.aiot.plugin.modbus.device.ModemConfig;
import com.zhny.aiot.plugin.modbus.device.ModemDeviceFactory;
import com.zhny.aiot.plugin.modbus.handler.model.ModemRequestMessage;
import com.zhny.aiot.plugin.model.CommandEntry;
import com.zhny.aiot.plugin.model.DeviceEntity;
import com.zhny.aiot.plugin.model.FactoryType;
import com.zhny.aiot.plugin.model.StoreEntity;
import org.pf4j.Extension;
import org.pf4j.Plugin;
import org.pf4j.PluginWrapper;

import java.util.ArrayList;
import java.util.List;

public class ControlCabinetPlugin extends Plugin {
    public ControlCabinetPlugin(PluginWrapper wrapper) {
        super(wrapper);
    }
    @Extension
    public static class ControlCabinetFactory extends ModemDeviceFactory<ControlCabinetStoreEntry> {

        @Override
        public List<CommandEntry> supportCommandClasses() {
            String adValveJson = getAdValveJson();
            List<CommandEntry> commands = new ArrayList();
            commands.add(CommandEntry.valueOf("openValve1", "打开电动蝶阀1","无参数", null));
            commands.add(CommandEntry.valueOf("closeValve1", "关闭电动蝶阀1","无参数", null));
            commands.add(CommandEntry.valueOf("openValve2", "打开电动蝶阀2","无参数", null));
            commands.add(CommandEntry.valueOf("closeValve2", "关闭电动蝶阀2","无参数", null));
            commands.add(CommandEntry.valueOf("setAdValve1", "调流阀1开度", "有参数",adValveJson));
            commands.add(CommandEntry.valueOf("setAdValve2", "调流阀2开度", "有参数",adValveJson));
            commands.add(CommandEntry.valueOf("powerSupply", "调流调压阀1供电", "无参数",null));
            commands.add(CommandEntry.valueOf("powerFailure", "调流调压阀1断电", "无参数",null));
            commands.add(CommandEntry.valueOf("queryValveStatus", "电动蝶阀状态检测", "无参数",null));
            commands.add(CommandEntry.valueOf("queryFlow", "检测流量", "无参数",null));
            commands.add(CommandEntry.valueOf("queryPreAndAdValve", "检测压力及开度检测", "无参数",null));
            return commands;
        }

        private String getAdValveJson(){
            String paramJson;
            ObjectMapper mapper = new ObjectMapper();
            mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
            try {
                paramJson = mapper.writeValueAsString(ConfigUtils.getConfigFields(AdjustableParam.class));
            } catch (Exception e) {
                return null;
            }
            return paramJson;
        }

        public IDevice createDevice(ICenterFacilities iCenterFacilities, DeviceEntity entity) {
            return new ControlCabinetDevice (ModemServer.getInstance ().getEngine () ,this,iCenterFacilities,entity);
        }

        public Class<? extends StoreEntity> storeClass() {
            return ControlCabinetStoreEntry.class;
        }

        public String getId() {
            return "zhny-control-cabinet-plugin";
        }

        public String getName() {
            return "智慧控制柜插件";
        }

        public FactoryType getType() {
            return FactoryType.PumpController;
        }


        @Override
        public ModemRequestMessage getRequestMessage(ModemConfig modemConfig) {
            return null;
        }
    }
}
